//
//  UIImage+imageAlpha.m
//  jjjy
//
//  Created by jinbao on 13-10-22.
//  Copyright (c) 2013年 jjjy. All rights reserved.
//

#import "UIImage+imageAlpha.h"

@implementation UIImage (imageAlpha)

/*
 @method     (UIImage *)sourceImage:(UIImage *)aImage applyingAlpha:(CGFloat)aAlpha
 @abstract   根据参数图片创建该图片 alpha 的图片
 @result     void
 */
+ (UIImage *)sourceImage:(UIImage *)aImage applyingAlpha:(CGFloat)aAlpha
{
    UIGraphicsBeginImageContextWithOptions(aImage.size, NO, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, aImage.size.width, aImage.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, aAlpha);
    CGContextDrawImage(ctx, area, aImage.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

/*
 @method     createImageWithColor:
 @abstract   创建指定颜色的图片
 @result     void
 */
+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

//调整图片大小
+(UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
@end

